<?php

namespace app\api\controller\kuyou\applet;

use app\api\model\kuyou\applet\Invoicing as Invoicings;

/**
 * 采购开票接口
 */
class Invoicing extends Common
{
    protected $noNeedRight = ['getInvoicing'];

    /**
     * 获取采购开票
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $name 采购开票名称
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?config('app_limit'):$limit;

            $where = [];
            $bill_time = $this->request->post('bill_time');
            if(!empty($bill_time)){
                $str = explode('~',$bill_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['bill_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $suppliers_code = $this->request->post('suppliers_code');
            if(!empty($suppliers_code)){
                $where['suppliers_code'] = ["like", '%'.$suppliers_code.'%'];
            }
            $bill_codes = $this->request->post('bill_codes');
            if(!empty($bill_codes)){
                if(strpos($bill_codes,',') !== false){
                    $str = explode(',',$bill_codes);
                    $where['bill_codes'] = ["in", $str];
                }else{
                    $where['bill_codes'] = ["like", '%'.$bill_codes.'%'];
                }
            }
            $invoice = $this->request->post('invoice');
            if(!empty($invoice)){
                $where['invoice'] = ["like", '%'.$invoice.'%'];
            }
            $invoice_header = $this->request->post('invoice_header');
            if(!empty($invoice_header)){
                $where['invoice_header'] = ["like", '%'.$invoice_header.'%'];
            }

            $sort_field = $this->request->post('sort_field');
            $sort_field = empty($sort_field)?'bill_codes':$sort_field;

            $sort_type = $this->request->post('sort_type');
            $sort_type = empty($sort_type)?'DESC':$sort_type;

            $result = Invoicings::getList($where,$page,$limit,$sort_field,$sort_type);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取可开票记录
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     */
    public function getInvoicing()
    {
        if ($this->request->isPost()) {

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;

            $where = [];
            $bill_time = $this->request->post('bill_time');
            if(!empty($bill_time)){
                $str = explode('~',$bill_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['bill_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $suppliers_code = $this->request->post('suppliers_code');
            if(!empty($suppliers_code)){
                $where['suppliers_code'] = ["like", '%'.$suppliers_code.'%'];
            }
            $bill_codes = $this->request->post('bill_codes');
            if(!empty($bill_codes)){
                if(strpos($bill_codes,',') !== false){
                    $str = explode(',',$bill_codes);
                    $where['bill_code'] = ["in", $str];
                }else{
                    $where['bill_code'] = ["like", '%'.$bill_codes.'%'];
                }
            }
            //$where['invoice_status'] = '0';
            $tax_status = $this->request->post('tax_status');
            if($tax_status==1){
                $where['tax'] = ['>',0];
            }

            $give_status = $this->request->post('give_status');
            if($give_status==1){
                $where['give_money'] = ['>',0];
            }else if($give_status==2){
                $where['give_money'] = 0;
            }

            $result = Invoicings::getInvoicing($where,$page,$limit);
            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加采购开票
     * @ApiMethod (POST)
     * @param array $detail 采购开票数据
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Invoicings::addDetail($data);
            if($ret['status']==200){
                $this->success('添加成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 删除采购开票
     * @ApiMethod (POST)
     * @param int $id 采购开票ID
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $ret = Invoicings::delDetail($id);
            if($ret['status']==200){
                $this->success('删除成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
